<?php

/**
 * crpCalendar
 *
 * @copyright (c) 2007,2009 Daniele Conca
 * @link http://code.zikula.org/crpcalendar Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpCalendar
 */

/**
 * crpCalendarUI, User Interface class for crpCalendar
 */
class crpCalendarUI
{

	/**
	 * constructor, set values
	 */
	function crpCalendarUI()
	{
		// none
		$this->theme_options = array (
			'default' => 'default',
			'tan' => 'tan',
			'grey' => 'grey',
			'green' => 'green'
		);

		switch (pnModgetVar('crpCalendar', 'crpcalendar_weekday_start'))
		{
			case "1" :
				$this->day_of_week_short = array (
					_CRPCALENDAR_MON,
					_CRPCALENDAR_TUE,
					_CRPCALENDAR_WED,
					_CRPCALENDAR_THU,
					_CRPCALENDAR_FRI,
					_CRPCALENDAR_SAT,
					_CRPCALENDAR_SUN
				);
				break;
			default :
				$this->day_of_week_short = array (
					_CRPCALENDAR_SUN,
					_CRPCALENDAR_MON,
					_CRPCALENDAR_TUE,
					_CRPCALENDAR_WED,
					_CRPCALENDAR_THU,
					_CRPCALENDAR_FRI,
					_CRPCALENDAR_SAT
				);
				break;
		}
	}

	/**
	 * draw events administration list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param int $active specified status
	 *
	 * @return string html code
	 */
	function adminList($rows = array (), $category = null, $mainCat = null, $modvars = array (), $active = null, $sortColumn = 'start_date', $sortOrder = 'DESC')
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		// Assign the items to the template
		$pnRender->assign('events', $rows);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign($modvars);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign('events_status', $active);
		$pnRender->assign('sortColumn', $sortColumn);
		$pnRender->assign('sortOrder', $sortOrder);

		// Assign the information required to create the pager
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpCalendar', 'user', 'countitems', array (
				'category' => $category,
				'active' => $active
			)),
			'itemsperpage' => $modvars['itemsperpage']
		));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_view.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param string $typeList upcoming/archive
	 *
	 * @return string html code
	 */
	function userList($rows = array (), $startnum = 1, $category = null, $mainCat = null, $modvars = array (), $typeList = null)
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $typeList . '|' . $startnum. '|' . $category;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_view.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_view.htm');
		}

		// Assign the items to the template
		$pnRender->assign('events', $rows);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);
		$pnRender->assign('typeList', $typeList);

		// Assign the information required to create the pager
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpCalendar', 'user', 'countitems', array (
				'category' => $category,
				'active' => 'A',
				'typeList' => $typeList
			)),
			'itemsperpage' => $modvars['itemsperpage']
		));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_view.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param int $t timestamp
	 * @param array $date year,month,day values
	 *
	 * @return string html code
	 */
	function userYearList($rows = array (), $category = null, $mainCat = null, $modvars = array (), $t = null, $date = null)
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = DateUtil :: getDatetime($t, '%Y') . '|' . $category;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_view_year.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_view_year.htm');
		}

		// Assign the items to the template
		$pnRender->assign('events', $rows);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);
		$pnRender->assign('t', $t);
		$pnRender->assign('date', $date);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_view_year.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $uid user identifier
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function userPartecipations($rows = array (), $uid = null, $category = null, $mainCat = null, $modvars = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $uid . '|' . $category;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_view_partecipations.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_view_partecipations.htm');
		}

		// Assign the items to the template
		$pnRender->assign('events', $rows);
		$pnRender->assign('uid', $uid);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);
		$pnRender->assign('user_avatar', pnUserGetVar('_YOURAVATAR', $uid));

		// Assign the information required to create the pager
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpCalendar', 'user', 'countitems', array (
				'category' => $category,
				'active' => 'A',
				'uid' => $uid
			)),
			'itemsperpage' => $modvars['itemsperpage']
		));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_view_partecipations.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function attendeesList($rows = array (), $category = null, $mainCat = null, $modvars = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $category;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_view_attendees.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_view_attendees.htm');
		}

		// add Profile define
		pnModLangLoad('Profile');

		// Assign the items to the template
		$pnRender->assign('users', $rows);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);
		/* TODO : 9999 -> $navigationValues['modvars'] to be changed when Zikula ticket #49 is resolved
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpCalendar',
			'user',
			'countitems_attendance',
			array (
				'category' => $category,
				'active' => 'A',
				'eventid' => true
			)
		), 'itemsperpage' => $modvars['itemsperpage']));
		*/
		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_view_attendees.htm');
	}

	/**
	 * draw user month events list
	 *
	 * @param array $items of event's value
	 * @param array $days of week's month dates
	 * @param array $daysexpanded out of week's month dates
	 * @param int $t timestamp
	 * @param array $date year,month,day values
	 * @param datetime $startDate init
	 * @param datetime $endDate end
	 * @param datetime $today for layout purpose
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param string $viewForm visualization
	 *
	 * @return string html code
	 */
	function userMonthList($items = array (), $days = array (), $daysexpanded = array (), $t = null, $date = null, $startDate = null, $endDate = null, $today = null, $category = null, $mainCat = null, $modvars = array (), $viewForm = 'table')
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $viewForm . '|' . DateUtil :: getDatetime($t, '%b') . '|' . $category;

		// check out if the contents are cached.
		switch ($viewForm)
		{
			case 'list' :
				if ($pnRender->is_cached('crpcalendar_user_view_month.htm'))
					return $pnRender->fetch('crpcalendar_user_view_month.htm');
				break;
			case 'table' :
			default :
				if ($pnRender->is_cached('crpcalendar_user_monthlist.htm'))
					return $pnRender->fetch('crpcalendar_user_monthlist.htm');
				break;
		}

		// Assign the items to the template
		$pnRender->assign('events', $items);
		$pnRender->assign('days', $days);
		$pnRender->assign('daysexpanded', $daysexpanded);
		$pnRender->assign('t', $t);
		$pnRender->assign('todayEv', $today);
		$pnRender->assign('date', $date);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);
		$pnRender->assign('viewForm', $viewForm);

		$pnRender->assign('day_of_week_short', $this->day_of_week_short);

		// Return the output that has been generated by this function
		switch ($viewForm)
		{
			case 'list' :
				return $pnRender->fetch('crpcalendar_user_view_month.htm');
				break;
			case 'table' :
			default :
				return $pnRender->fetch('crpcalendar_user_monthlist.htm');
				break;
		}
	}

	/**
	 * draw user week events list
	 *
	 * @param array $items of event's value
	 * @param array $days of week's month dates
	 * @param array $daysexpanded out of week's month dates
	 * @param int $t timestamp
	 * @param array $date year,month,day values
	 * @param datetime $startDate init
	 * @param datetime $endDate end
	 * @param datetime $today for layout purpose
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param string $viewForm visualization
	 *
	 * @return string html code
	 */
	function userWeekList($items = array (), $days = array (), $daysexpanded = array (), $t = null, $date = null, $startDate = null, $endDate = null, $today = null, $category = null, $mainCat = null, $modvars = array (), $viewForm = 'table')
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $viewForm . '|' . DateUtil :: getDatetime($t, '%U') . '|' . $category;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_weeklist.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_weeklist.htm');
		}

		// Assign the items to the template
		$pnRender->assign('events', $items);
		$pnRender->assign('days', $days);
		$pnRender->assign('daysexpanded', $daysexpanded);
		$pnRender->assign('t', $t);
		$pnRender->assign('date', $date);
		$pnRender->assign('todayEv', $today);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		$pnRender->assign('day_of_week_short', $this->day_of_week_short);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_weeklist.htm');
	}

	/**
	 * draw user day events list
	 *
	 * @param array $items of event's value
   * @param int $startnum page first item counter
   * @param int $numitems total viewable items
	 * @param array $day to display
	 * @param int $t timestamp
	 * @param array $date year,month,day values
	 * @param datetime $startDate init
	 * @param datetime $endDate end
	 * @param datetime $today for layout purpose
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function userDayList($items = array (), $startnum=1, $day = null, $t = null, $date = null, $startDate = null, $endDate = null, $today = null, $category = null, $mainCat = null, $modvars = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = DateUtil :: getDatetime($t, '%Y%m%d') . '|' . $category . '|' . $startnum;

		// check out if the contents are cached.
		if ($pnRender->is_cached('crpcalendar_user_daylist.htm'))
		{
			return $pnRender->fetch('crpcalendar_user_daylist.htm');
		}

		// Assign the items to the template
		$pnRender->assign('events', $items);
		$pnRender->assign('day', $day);
		$pnRender->assign('t', $t);
		$pnRender->assign('date', $date);
		$pnRender->assign('todayEv', $today);
		$pnRender->assign('events_category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		$pnRender->assign('day_of_week_short', $this->day_of_week_short);

    if (!$modvars['daylist_categorized'])
		{
      // Assign the information required to create the pager
      $pnRender->assign('pager', array (
        'numitems' => pnModAPIFunc('crpCalendar', 'user', 'countitems', array (
          'category' => $category,
          'active' => 'A',
          'startDate' => $startDate,
          'endDate' => $endDate
        )),
        'itemsperpage' => $modvars['itemsperpage']
      ));
    }
		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_daylist.htm');
	}

	/**
	 * draw single event page
	 *
	 * @param int $eventid identifier
	 * @param array $item of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function userDisplay($eventid = null, $item = array (), $modvars = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// determine which template to render this page with
		// A specific template may exist for this page (based on page id)
		if ($pnRender->template_exists("crpcalendar_user_display_$eventid"))
		{
			$template = "crpcalendar_user_display_$eventid";
		}
		else
		{
			$template = 'crpcalendar_user_display.htm';
		}

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $eventid ;

		// check out if the contents are cached.
		if ($pnRender->is_cached($template))
		{
			return $pnRender->fetch($template);
		}

		// Assign the items to the template
		$pnRender->assign('event', $item);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		// Return the output that has been generated by this function
		return $pnRender->fetch($template);
	}

	/**
	 * draw single event page for Content module
	 *
	 * @param int $eventid identifier
	 * @param array $item of event's value
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function userSimpleDisplay($eventid = null, $item = array (), $modvars = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar');

		// determine which template to render this page with
		// A specific template may exist for this page (based on page id)
		if ($pnRender->template_exists("crpcalendar_user_simple_display_$eventid"))
		{
			$template = "crpcalendar_user_simple_display_$eventid";
		}
		else
		{
			$template = 'crpcalendar_user_simple_display.htm';
		}

		// For caching reasons you must pass a cache ID
		$pnRender->cache_id = $eventid ;

		// check out if the contents are cached.
		if ($pnRender->is_cached($template))
		{
			return $pnRender->fetch($template);
		}

		// Assign the items to the template
		$pnRender->assign('event', $item);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		// Return the output that has been generated by this function
		return $pnRender->fetch($template);
	}

	/**
	 * draw creation form
	 *
	 * @param array $item of event's value
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param array $avail available locations
	 *
	 * @return string html
	 */
	function newEvent($item = array (), $mainCat, $modvars, $avail = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign($modvars);
		$pnRender->assign($item);
		$pnRender->assign('avail', $avail);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_new.htm');
	}

	/**
	 * draw user creation form
	 *
	 * @param array $item of event's value
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param array $avail available locations
	 *
	 * @return string html
	 */
	function submitEvent($item = array (), $mainCat, $modvars, $avail = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign($modvars);
		$pnRender->assign($item);
		$pnRender->assign('avail', $avail);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_new.htm');
	}

	/**
	 * draw modify form
	 *
	 * @param array $item of event's value
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param array $avail available locations
	 *
	 * @return string html
	 */
	function modifyEvent($item, $mainCat, $modvars, $avail = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign($modvars);

		if (SecurityUtil :: checkPermission("crpCalendar::", "$item[cr_uid]:$item[title]:$item[eventid]", ACCESS_DELETE))
		{
			$deletelink = array (
				'url' => pnModURL('crpCalendar', 'admin', 'delete', array (
					'eventid' => $item['eventid']
				)),
				'image' => 'editdelete.gif',
				'title' => _DELETE
			);

			$pnRender->assign('deletelink', $deletelink);
		}

		// assign the item to the template
		$pnRender->assign('eventid', $item['eventid']);
		$pnRender->assign($item);
		$pnRender->assign('avail', $avail);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_modify.htm');
	}

	/**
	 * draw modify form
	 *
	 * @param array $item of event's value
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 * @param array $avail available locations
	 *
	 * @return string html
	 */
	function editEvent($item, $mainCat, $modvars, $avail = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign($modvars);

		// assign the item to the template
		$pnRender->assign('eventid', $item['eventid']);
		$pnRender->assign($item);
		$pnRender->assign('avail', $avail);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_user_edit.htm');
	}

	/**
	 * draw delete form
	 *
	 * @param int $eventid item identifier
	 *
	 * @return string html
	 */
	function deleteEvent($eventid)
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		// assign the item to the template
		$pnRender->assign('eventid', $eventid);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_delete.htm');
	}

	/**
	 * draw modify configuration form
	 *
	 * @param array $gdArray gd library info
	 * @param array $modvars module's variables
	 *
	 * @return string html
	 */
	function modifyConfig($modvars = array (), $gdArray = array ())
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign($modvars);
		$pnRender->assign('theme_options', $this->theme_options);
		$pnRender->assign('gd_version', $this->gd_version($gdArray['GD Version']));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_modifyconfig.htm');
	}

	/**
	 * draw rss feed
	 *
	 * @param array $data feed values
	 * @param array $list elements
	 *
	 * @return string xml
	 */
	function drawFeed($data, $list, $modvars = array ())
	{
		$pnRender = pnRender :: getInstance('crpCalendar', false);
		$pnRender->assign('data', $data);
		$pnRender->assign('list', $list);
		$pnRender->assign($modvars);

		return $pnRender->fetch('crpcalendar_user_getfeed.htm');
	}

	/**
	 * draw ical event export
	 *
	 * @param array $data event values
	 *
	 * @return string html
	 */
	function drawICal($data = array ())
	{
		$pnRender = pnRender :: getInstance('crpCalendar', false);
		$pnRender->assign('data', $data);

		return $pnRender->fetch('ical/crpcalendar_user_getical.htm');
	}

	/**
	 * draw ical header export
	 *
	 * @param string $selfurl return url
	 *
	 * @return string html
	 */
	function drawICalHeader($selfurl = null)
	{
		$pnRender = pnRender :: getInstance('crpCalendar', false);
		$pnRender->assign('selfurl', $selfurl);

		return $pnRender->fetch('ical/crpcalendar_user_ical_header.htm');
	}

	/**
	 * draw ical footer export
	 *
	 * @return string html
	 */
	function drawICalFooter()
	{
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		return $pnRender->fetch('ical/crpcalendar_user_ical_footer.htm');
	}

	/**
	 * calculate gd library information
	 *
	 * @return array gd library info
	 */
	function gd_version($fullstring = null)
	{
		$cache_gd_version = array ();

		if (eregi('bundled \((.+)\)$', $fullstring, $matches))
		{
			$cache_gd_version['string'] = $fullstring; // e.g. "bundled (2.0.15 compatible)"
			$cache_gd_version['value'] = (float) $matches[1]; // e.g. "2.0" (not "bundled (2.0.15 compatible)")
		}
		else
		{
			$cache_gd_version['string'] = $fullstring; // e.g. "1.6.2 or higher"
			$cache_gd_version['value'] = (float) substr($fullstring, 0, 3); // e.g. "1.6" (not "1.6.2 or higher")
		}

		return $cache_gd_version;
	}

	/**
	 * draw import ical form
	 *
	 * @return string html
	 */
	function drawImportForm($mainCat)
	{
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		$pnRender->assign('mainCategory', $mainCat);

		return $pnRender->fetch('ical/crpcalendar_admin_import_ics.htm');
	}

	/**
	 * draw purge form
	 *
	 * @return string html
	 */
	function drawPurgeEvents()
	{
		// Create output object
		$pnRender = pnRender :: getInstance('crpCalendar', false);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpcalendar_admin_purge.htm');
	}

}